﻿<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';
require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';

header('Content-Type: text/plain');


//$action = MiscUtils::getParam('name', NULL);

$class = MiscUtils::getParam('name', NULL);
$objectsToAddOrUpdate = json_decode(MiscUtils::getParam('obj', NULL));
$objectsToRemove = json_decode(MiscUtils::getParam('r', NULL));

/*echo $class;
print_r($objectsToAddOrUpdate);
print_r($objectsToRemove);
die;*/

    foreach ($objectsToAddOrUpdate as &$object) {
      if (isset($object->id) || isset($object->e_oid)) {
      	
      	switch ($class)
      	{
      		case'producenote':
      			pdoproducenote($object,$class);
      			break;

      		case 'produce':
      			pdoproduceupdate($object,$class);
      			break;	
      			
            case 'produce_user':
                pdoproduceuserupdate($object,$class);
                break;
                        			
      		default:
      			pdoupdate($object,$class);
      			break;
      	}
        
      } else {
      	switch ($class)
      	{
      		case 'producenote':
                pdoproducenote($object,$class);
                break;
                
            case 'produce_user':
                pdoproduceuserupdate($object,$class);
                break;
                
      		default:
      	        pdoadd($object,$class);		
      			break;
      	}
        
      }
    }
    foreach ($objectsToRemove as &$object) {
      pdoremove($object,$class);
    }
       

function pdoadd($obj,$class,$debug=''){
	
	
	//$addsql="insert into ns_".$class."  values(NULL,";
	
	$addsqlfield = '';
	$addsqlvalue = '';
	
	foreach ($obj as $key => $value)
	{
		$addsqlfield .='`'.$key.'`,';
		
		if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','when','date')))
		{
			if(is_object($obj->$key))
			{
				$value = SimpleDate::toStamp($obj->$key);
				if(empty($value)) $value = 0;
			}else{
				if(empty($obj->$key)) $value = 0;
			}
		}
		
	   $addsqlvalue.=" '".addslashes($value)."',";
	}

	$addsqlfield =substr($addsqlfield,0,-1);
	$addsqlvalue =substr($addsqlvalue,0,-1);
	   
	$addsql =" insert into ns_".$class." (`e_oid`,".$addsqlfield.")  values(NULL,".$addsqlvalue.")";
   
	if(!empty($debug))
	{
		echo $addsql."\n";
	}else{   
	    sql_execute($addsql);
	}
	
}
    
    
function pdoupdate($obj,$class){
	
	$id=$obj->id;
	
	$upsql="update ns_".$class." set ";
	
	foreach ($obj as $key => $value){
		
		if(!in_array($key ,array('id','e_oid','trackId') )){
			$upsql.= $key."='".addslashes($value)."',";
		}
		
	}
	
	
	$upsql =substr($upsql,0,-1);
	$upsql.=" where e_oid='".$id."'";
	
	
   sql_execute($upsql);
	
}


function pdoremove($obj,$class){
	
	$id = $obj->id;
	$delsql="delete from ns_".$class." where e_oid='".$id."'";
	
	sql_execute($delsql);
	
}


/**
 * 
 * pdoproducenote operate
 */
 function pdoproducenote($obj,$class,$debug='')
 {
 	if(!empty($debug))
 	{
 		print_r($obj);
 	}
 	
 	if(!empty($obj))
 	{
 		if(isset($obj->id) || isset($obj->e_oid) ) 
 		{
 			$producesql = "update ns_".$class." set cancelled = '".$obj->cancelled."' where e_oid = '".$obj->e_oid."'";
 			if(!empty($debug))
 			{
          	    echo $producesql."\n";			
 			}else{
 		 	    sql_execute($producesql);
 			}
 			
 		}else{
            $producesql = "insert into ns_".$class."(`e_oid`,`trackId`,`produce_trackId`,`author`,`date`,`content`,`cancelled`) values(NULL,'".$obj->trackId."','".$obj->produce_trackId."',
                                                    '".$obj->author."','".SimpleDate::toStamp($obj->date)."','".addslashes($obj->content)."','".$obj->cancelled."')";
 		    if(!empty($debug))
            {
                echo $producesql."\n";          
            }else{
                sql_execute($producesql);
            }
             			
 		}
 		
 		
 	}
 	
 }
 
 /**
  * pdoproduceupdate
  * 
  */
 
  function pdoproduceupdate($obj,$tablename,$debug='')
 {
    if(!empty($debug))
    {
        print_r($obj);
    }
    
    if (!empty($obj))
    {
        
        $producefield = "update ns_".$tablename." set ";
        
        foreach ($obj as $key => $value)
        {
            if(!in_array($key,array('pi_selects','v2_purchaseItems','pUsers','v2_events','v2_tracks','v2_produces','v2_notes','id','e_oid','_text','when','deadline','_exist')))
            {
                if(in_array($key,array('deadline','date','createdDate','sentDate','receivedDate')))
                {
                	if(is_object($obj->$key))
                	{
                		$value = SimpleDate::toStamp($value);
                		
                	}else{
                		
                		if(empty($obj->$key)) $value = 0;
                	}
                }
                
                $producefield .= '`'.$key."`='".addslashes($value)."',";
            }
            

           if(in_array($key,array('when','deadline')))
           {
               if(empty($obj->$key)) 
               {
                  $obj->$key = 0;
                  $producefield .= " `$key` = '".$obj->$key."',";
               }else{
                  $producefield .= " `$key` = '".SimpleDate::toStamp($obj->$key)."',";
               }
                    
               
           }                    
            
        }
        
        $producefield = substr($producefield,0,-1);
        $producefield .=" where e_oid = '".$obj->e_oid."'";
        
        if(!empty($debug))
        {
            echo $producefield."\n";
        }else{
            sql_execute($producefield);
        }
        
        /**
         * pi_selects operate
         * 
         */
        if(isset($obj->pi_selects))
        {
            if(!empty($obj->pi_selects))
            {
               pi_selects_operate($obj->pi_selects,$debug);
                
            }//not null
            
        }//是否存在pi_selects
        
        /**
         * v2_purchaseItems operate
         */
        if(isset($obj->v2_purchaseItems))
        {
            if(!empty($obj->v2_purchaseItems))
            {
                v2_purchaseItems_operate($obj->v2_purchaseItems,$debug);
            }
            
        }//是否存在v2_purchaseItems  end v2_purchaseItems operate

        if(isset($obj->v2_pUsers))
        {
            if(!empty($obj->v2_pUsers))
            {
                pusers_operate($obj->v2_pUsers,$debug);
                
            }//$obj->pUsers not null
            
        }//是否存在pUsers，  end pUsers operate
        
        /**
         * v2_events
         */
        if(isset($obj->v2_events))
        {
            if(!empty($obj->v2_events))
            {
                v2_events_operate($obj->v2_events,$debug);
            }
            
        }//是否存在v2_events ，end v2_events operate

        if(isset($obj->v2_tracks))
        {
            if(!empty($obj->v2_tracks))
            {
                v2_tracks_operate($obj->v2_tracks,$debug);
            }
            
        }//exist  v2_tracks ,end v2_tracks operate
        
        if(isset($obj->v2_notes))
        {
            if(!empty($obj->v2_notes))
            {
                v2_notes_operate($obj->v2_notes,$debug);
                
            }//v2_notes not null
            
            
        }//exist notes note null
        
        if(isset($obj->v2_produces))
        {
            if(!empty($obj->v2_produces))
            {
                foreach ($obj->v2_produces as $v2produce)
                {
                    if(isset($v2produce->id) || isset($v2produce->e_oid))
                    {//update 
                        $v2producesql = 'update ns_produce set ';
                        
                        foreach($v2produce as $key => $value)
                        {
                            if(!in_array($key,array('pi_selects','v2_purchaseItems','pUsers','v2_events','v2_tracks','v2_produces','v2_notes','id','e_oid','_text','when','deadline')))
                            {
                               if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date'))) $value = SimpleDate::toStamp($value);
                               $v2producesql .= '`'.$key."`='".addslashes($value)."',";
                            }  

                           if(in_array($key,array('when','deadline')))
                           {
                               if(empty($v2produce->$key))
                               {
                                  $v2produce->$key = 0;
                                  $v2producesql .= " `$key` = '".$v2produce->$key."',";
                               }else{
                                   $v2producesql .= " `$key` = '".SimpleDate::toStamp($v2produce->$key)."',";   
                               }
                                    
                               
                           }                             
                            
                        }
                        
                        
                        /*if(in_array($key,array('when')))
                        {
                            $v2producesql .='`when`,'; 
                            
                            if(empty($v2produce->when)) $v2produce->when = 0;
                            
                            $v2producesql .= " '".$v2produce->when."',";
                        } */

                        
                        
                        $v2producesql =substr($v2producesql,0,-1);
                        
                        $v2producesql .= " where e_oid = '".$v2produce->e_oid."'";
                        
                        if(!empty($debug))
                        {
                            echo $v2producesql."\n";
                        }else{
                            sql_execute($v2producesql);
                        }
                        unset($key);
                        unset($value);

                        /**
                         * pi_selects
                         */
                        if(isset($v2produce->pi_selects))
                        {
                            if(!empty($v2produce->pi_selects))
                            {
                                pi_selects_operate($v2produce->pi_selects,$debug);
                            }
                        }
                        
                        /**
                         * v2_purchaseItems
                         */
                        if(isset($v2produce->v2_purchaseItems))
                        {
                            if(!empty($v2produce->v2_purchaseItems))
                            {
                                v2_purchaseItems_operate($v2produce->v2_purchaseItems,$debug);
                            }
                        }
                        
                        
                    }else{//add
                        $v2producefield = '';
                        $v2producevalue = '';
                        
                        foreach($obj->v2_produces as $key => $value)
                        {
                            if(!in_array($key,array('pi_selects','v2_pUsers','v2_purchaseItems','v2_events','v2_tracks','v2_produces','v2_notes','_exist','when','deadline')))
                            {
                                $v2producefield .='`'.$key.'`,';
            
                                if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date'))) $value = SimpleDate::toStamp($value);
                                    
                                $v2producevalue .= " '".addslashes($value)."',";                      
                            
                            }
            
                            if(in_array($key,array('when','deadline')))
                            {
                                $v2producefield .='`$key`,'; 
                                
                                if(empty($obj->$key))
                                {
                                    $obj->$key = 0;
                                    $v2producevalue .= " '".$obj->$key."',";
                                    
                                }else{
                                    $v2producevalue .= " '".SimpleDate::toStamp($obj->$key)."',";   
                                }
                                
                                
                            }               
                        
                        
                        }
            
                        $v2producefield = substr($v2producefield,0,-1);
                        $v2producevalue = substr($v2producevalue,0,-1);
            
                        $v2producesql = "insert into ns_produce(`e_oid`,".$v2producefield.") values(NULL,".$v2producevalue.")";  
                         
                        if(!empty($debug))
                        {
                            echo $v2producesql."\n";         
                        }else{
                            sql_execute($v2producesql);
                        } 
                        
                        unset($key);
                        unset($value);

                        /**
                         * pi_selects
                         */
                                            /**
                         * pi_selects
                         */
                        if(isset($v2produce->pi_selects))
                        {
                            if(!empty($v2produce->pi_selects))
                            {
                                pi_selects_operate($v2produce->pi_selects,$debug);
                            }
                        }
                        
                        /**
                         * v2_purchaseItems
                         */
                        if(isset($v2produce->v2_purchaseItems))
                        {
                            if(!empty($v2produce->v2_purchaseItems))
                            {
                                v2_purchaseItems_operate($v2produce->v2_purchaseItems,$debug);
                            }
                        }
                        
                    }//end add
                }
            }
            
            
        }//exist v2_produces ,end v2_produces operate
        
        
    }//obj not null
    
 }//end pdoupdate

 
 /**
  * update produce user email
  */
 
  function pdoproduceuserupdate($obj,$tablename,$debug='')
  {
  	if(!empty($debug))
  	{
  		print_r($obj);
  	}
  	
  	if(!empty($obj))
  	{
  		if(isset($obj->id) ||isset($obj->e_oid))
  		{
	  		$sql="update ns_produce set user_email = '{$obj->email}' where branch = '{$obj->branch}' and user_trackId = '{$obj->trackId}'";
	  		
	  		if(!empty($debug))
	  		{
	  			echo $sql."\n";
	  		}else{
	  			sql_execute($sql);
	  		}
  		}
  		
  	}
  	
  	
  }
 
 
 
 



?>